package com.xxx.video.main.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.main.presenters.ISearchView;
import com.xxx.video.main.presenters.SearchPresenter;
import com.xxx.video.mine.fragments.SearchRecommendFragment;
import com.xxx.video.mine.fragments.SearchResultListFragment;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

public class SearchActivity extends BaseActivity<SearchPresenter> implements ISearchView {

    @BindView(R.id.et_search_activity_actionbar_search_content)
    EditText etSearchActivityActionbarSearchContent;
    @BindView(R.id.ib_search_activity_actionbar_search_delete)
    ImageButton ibSearchActivityActionbarSearchDelete;
    @BindView(R.id.cl_ranking_fragment_top_actionbar)
    ConstraintLayout clRankingFragmentTopActionbar;
    /**
     * this is a search recommend list fragment
     */
    private SearchRecommendFragment mSearchRecommendFragment;
    /**
     * this is a search result list fragment
     */
    private SearchResultListFragment mSearchResultListFragment;

    /**
     * Recommend tag
     */
    private static final String SEARCH_RECOMMEND = "Search_Recommend";
    /**
     * Result tag
     */
    private static final String SEARCH_RESULT = "Search_Result";
    /**
     * is show search result list fragment
     */
    private boolean isShowResultList;

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) clRankingFragmentTopActionbar.getLayoutParams();
        layoutParams.setMargins(0, StatusBarUtil.getInstance().getStatusBarHeight(Objects.requireNonNull(this)), 0, 0);
        clRankingFragmentTopActionbar.setLayoutParams(layoutParams);
    }

    @Override
    protected SearchPresenter createPresenter() {
        return new SearchPresenter(this, this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_search;
    }

    @Override
    public void initData(Bundle extras) {
        showRootLayoutStatus(1);
        showSearchRecommendListFragment();
        initSearchEditTextListener();
    }

    @OnTextChanged(R.id.et_search_activity_actionbar_search_content)
    public void onTextChanged() {
        if (TextUtils.isEmpty(getSearchContent())) {
            ibSearchActivityActionbarSearchDelete.setVisibility(View.GONE);
        } else {
            ibSearchActivityActionbarSearchDelete.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public String getSearchContent() {
        return etSearchActivityActionbarSearchContent.getText().toString().trim();
    }

    @Override
    public void initSearchEditTextListener() {
        etSearchActivityActionbarSearchContent.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                if (!TextUtils.isEmpty(getSearchContent())) {
                    showSearchResultListFragment();
                    if (mSearchResultListFragment != null) {
                        mSearchResultListFragment.setSearchKeyWords(getSearchContent());
                    }
                }
                return true;
            }
            return false;
        });
    }

    @OnClick({R.id.ib_search_activity_actionbar_back, R.id.tv_search_activity_actionbar_search, R.id.ib_search_activity_actionbar_search_delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_search_activity_actionbar_back:
                onBackPressed();
                break;
            case R.id.tv_search_activity_actionbar_search:
                showSearchResultListFragment();
                if (mSearchResultListFragment != null) {
                    mSearchResultListFragment.setSearchKeyWords(getSearchContent());
                }
                break;
            case R.id.ib_search_activity_actionbar_search_delete:
                etSearchActivityActionbarSearchContent.setText("");
                break;
            default:
                break;
        }
    }

    @Override
    public void showSearchRecommendListFragment() {
        /*
         * if fragment ==null else create
         */
        if (null == mSearchRecommendFragment) {
            mSearchRecommendFragment = SearchRecommendFragment.newInstance(new Bundle());
            addFragment(R.id.fl_search_activity_container, mSearchRecommendFragment, SEARCH_RECOMMEND);
        }
        /*
         * hide all fragment
         */
        hideAllFragment();
        /*
         * show fragment
         */
        showFragment(mSearchRecommendFragment);
        isShowResultList = false;
    }

    @Override
    public void showSearchResultListFragment() {
        /*
         * if fragment ==null else create
         */
        if (null == mSearchResultListFragment) {
            mSearchResultListFragment = SearchResultListFragment.newInstance(new Bundle());
            addFragment(R.id.fl_search_activity_container, mSearchResultListFragment, SEARCH_RESULT);
        }
        /*
         * hide all fragment
         */
        hideAllFragment();
        /*
         * show fragment
         */
        showFragment(mSearchResultListFragment);
        isShowResultList = true;
    }

    @Override
    public void onBackPressed() {
        if (isShowResultList) {
            showSearchRecommendListFragment();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void hideAllFragment() {
        if (null != mSearchRecommendFragment) {
            hideFragment(mSearchRecommendFragment);
        }
        if (null != mSearchResultListFragment) {
            hideFragment(mSearchResultListFragment);
        }
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    public void search(String fKeyword) {
        try {
            etSearchActivityActionbarSearchContent.setText(fKeyword);
            etSearchActivityActionbarSearchContent.setSelection(fKeyword.length());
        } catch (Exception e) {
            e.printStackTrace();
        }
        showSearchResultListFragment();
        if (mSearchResultListFragment != null) {
            mSearchResultListFragment.setSearchKeyWords(getSearchContent());
        }
    }
}
